package com.starpl.mobile.android.monthunsae.november.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.starpl.mobile.android.monthunsae.november.activity.WebViewActivity;
import com.starpl.mobile.android.platform.common.base.BaseApplication;

/**
 * 일반적인 웹뷰의 처리가 아닌 추가적인 처리가 필요할 경우에 사용하는 웹뷰 처리용 클라이언트
 * 
 * @author 박우람
 * @since 2011.08.23
 */
public class EventWebViewClient extends WebViewClient
{
	private static final String TAG = "EventWebViewClient";
	private WebViewActivity mParentActivity;
	
	public EventWebViewClient(WebViewActivity parentActivity)
	{
		mParentActivity = parentActivity;
	}
	
	/* (non-Javadoc)
	 * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
	 */
	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url)
	{
		Log.e(TAG, "url : " + url);
		
		if(url != null)
		{
			Log.e(TAG, "이벤트 처리: " + url);

			// 마켓 링크 이동
			if(url.startsWith("market://") || url.startsWith("tstore://"))
			{
				Intent market_intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				mParentActivity.startActivity(market_intent);
			}
			// 웹뷰 엑티비티 닫기
			else if(url.contains("/shop/close/web"))
			{
				mParentActivity.finish();
			}
			// 친구에게 알리기 기능 호출
			else if(url.contains("/introduce_app_to_friend/"))
			{
				int positionTitle = url.indexOf("/title/");
				int positionMessage = url.indexOf("/message/");
				int positionUrl = url.indexOf("/url/");
				
				String paramTitle = url.substring(positionTitle + 7, positionMessage);
				String paramMessage = url.substring(positionMessage + 9, positionUrl);
				String paramUrl = url.substring(positionUrl + 5);
				
				try
				{
					paramTitle = URLDecoder.decode(paramTitle, "UTF-8");
					paramMessage = URLDecoder.decode(paramMessage, "UTF-8");
					paramUrl = URLDecoder.decode(paramUrl, "UTF-8");
				}
				catch(UnsupportedEncodingException e)
				{
				}
				
				mParentActivity.updateEventMessage(paramTitle, paramMessage, paramUrl);
				mParentActivity.registerForContextMenu(mParentActivity.getCurrentFocus());
				mParentActivity.openContextMenu(mParentActivity.getCurrentFocus());
				mParentActivity.unregisterForContextMenu(mParentActivity.getCurrentFocus());
			}
			else if(url.contains("/close/"))
			{
				mParentActivity.finish();
			}
			else if(url.startsWith("mailto:"))
			{
				Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
//				intent.putExtra(Intent.EXTRA_SUBJECT, subject);
//				intent.putExtra(Intent.EXTRA_TEXT, message);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mParentActivity.startActivity(intent);
			}
			else
			{
				return super.shouldOverrideUrlLoading(view, updateHeader(url));
			}
			
			
			return true;
		}
		else
		{
			return super.shouldOverrideUrlLoading(view, updateHeader(url));
		}
	}
	
	/**
	 * 주소에 필요한 추가 정보를 업데이트 합니다.
	 * 
	 * @param url 주소
	 * @return 추가 정보가 더해진 URL
	 */
	private String updateHeader(String url)
	{
		String brandService = BaseApplication.getConfigPersistent("brandService");
		
		if(url.contains("?"))
		{
			url = url + "&brandService=" + brandService;
		}
		else
		{
			url = url + "?brandService=" + brandService;
		}
		
		return url;
	}
}